package api

import (
	"gitee.com/shiwjlinux/arm/apps/instance"
	"gitee.com/shiwjlinux/arm/common"
	"gitee.com/shiwjlinux/arm/response"
	"github.com/gin-gonic/gin"
)

func (h *ApiHandler) Registry(appRouter gin.IRouter) {
	appRouter.GET("/", h.QueryInstance)
	appRouter.GET("/:ID", h.DescribeInstance)

	appRouter.POST("/", h.CreateInstance)
	appRouter.DELETE("/:ID", h.DeleteInstance)
	appRouter.POST(":ID/heartbeat", h.UpdateHeartbeat)
	appRouter.POST("/heartbeats", h.UpdateHeartbeats)

}

func (h *ApiHandler) QueryInstance(ctx *gin.Context) {
	req := instance.NewQueryInstanceRequest()
	pageRequest := common.NewPageRequest()
	req.PageRequest = pageRequest.PageRequestFromGinCtx(ctx)

	req.Keywords = ctx.Query("keywords")
	req.Tags = common.ParseTags(ctx.Query("tags"))
	req.ID = ctx.Query("ID")
	req.Sn = ctx.Query("sn")

	h.logger.Debug().Str("req.ID", req.ID).Str("req.Sn", req.Sn).Msgf("api query instance")
	// h.logger.Debug().Str("req.ID", req.ID).Str("req.Sn", req.Sn).Msgf("%+v", req)

	set, err := h.svc.QueryInstance(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	// h.logger.Debug().Str("req.ID", req.ID).Str("req.Sn", req.Sn).Msgf("%+v", set)

	response.Success(ctx, set)
}

func (h *ApiHandler) CreateInstance(ctx *gin.Context) {
	req := instance.NewInstance()
	// if err := common.Validate(req); err != nil {
	// 	h.logger.Err(err).Msg("validate request failed")
	// 	response.Failed(ctx, err)
	// 	return
	// }

	if err := ctx.Bind(req); err != nil {
		response.Failed(ctx, err)
		return
	}

	ins, err := h.svc.CreateInstance(ctx, req)
	if err != nil {
		h.logger.Err(err).Msg("(h *ApiHandler) CreateInstance failed")
		response.Failed(ctx, err)
		return
	}
	// h.logger.Debug().Msgf("(h *ApiHandler) CreateInstance request %+v ", req)

	response.Success(ctx, ins)
}

func (h *ApiHandler) DeleteInstance(ctx *gin.Context) {
	req := instance.NewDeleteInstanceRequest([]string{ctx.Param("ID")})
	ins, err := h.svc.DeleteInstance(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, ins)
}

func (h *ApiHandler) DescribeInstance(ctx *gin.Context) {
	req := instance.NewDescribeInstanceRequest(ctx.Param("ID"))
	ins, err := h.svc.DescribeInstance(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, ins)
}

func (h *ApiHandler) UpdateHeartbeat(ctx *gin.Context) {
	req := instance.NewUpdateHeartbeatRequest(ctx.Param("ID"))
	if err := ctx.Bind(req.Heartbeat); err != nil {
		response.Failed(ctx, err)
		return
	}
	ins, err := h.svc.UpdateHeartbeat(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, ins)
}

func (h *ApiHandler) UpdateHeartbeats(ctx *gin.Context) {
	req := instance.NewUpdateHeartbeatRequests([]string{})
	if err := ctx.Bind(req); err != nil {
		response.Failed(ctx, err)
		return
	}

	h.logger.Debug().Msgf("%v", req)

	err := h.svc.UpdateHeartbeats(ctx, req)
	if err != nil {
		response.Failed(ctx, err)
		return
	}
	response.Success(ctx, nil)
}
